#include "MaterialForm.h"
#include "ui_MaterialForm.h"

MaterialForm::MaterialForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MaterialForm)
{
    ui->setupUi(this);

    connect(ui->ambientForm,    SIGNAL(colorChanged(const QVector3D&)), SLOT(componentChanged(const QVector3D&)));
    connect(ui->diffuseForm,    SIGNAL(colorChanged(const QVector3D&)), SLOT(componentChanged(const QVector3D&)));
    connect(ui->specularForm,   SIGNAL(colorChanged(const QVector3D&)), SLOT(componentChanged(const QVector3D&)));
    connect(ui->boxShininess,   SIGNAL(valueChanged(double)), SLOT(componentChanged(double)));
}

MaterialForm::~MaterialForm()
{
    delete ui;
}

void MaterialForm::initMaterial(const Material &material)
{
    ui->ambientForm ->initColor(material.ambient);
    ui->diffuseForm ->initColor(material.diffuse);
    ui->specularForm->initColor(material.specular);
    ui->boxShininess->setValue(static_cast<double>(material.shininess));
}

void MaterialForm::componentChanged(double value)
{
    Q_UNUSED(value)

    Material material;

    material.ambient = ui->ambientForm->color();
    material.diffuse = ui->diffuseForm->color();
    material.specular = ui->specularForm->color();
    material.shininess = static_cast<float>(ui->boxShininess->value());

    emit materialChanged(material);
}

void MaterialForm::componentChanged(const QVector3D &color)
{
    Q_UNUSED(color);
    componentChanged(0.0);
}
